

#include "libEink_Spectra6_render.h"
#include "bmpView_JY.h"
#include "timeInterval_JY.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#ifndef nullptr
#define nullptr (void *)NULL
#endif // nullptr

static char *LUT_PATH = "../Eink_Spectra6_render/lut/Spectra6_Render_LUT_Default_v2.bin";
static EinkDitheringMethod DITHER = EINK_STUCKI_SERPENTINE;
static int MODE = 2;

int readBinaryFile_JY(const char *filename, uint8_t **fileBuffer, int *fileSize)
{
    *fileBuffer = (uint8_t *)nullptr;

    FILE *fp_s = NULL;
    fp_s = fopen(filename, "rb");

    if (fp_s == NULL)
    {
        printf("ERROR: Failed to open LUT file %s \n", filename);
        return -1;
    }
    // Check file size
    fseek(fp_s, 0, SEEK_END);
    *fileSize = ftell(fp_s);
    fseek(fp_s, 0, SEEK_SET);
    *fileBuffer = (uint8_t *)malloc(sizeof(uint8_t) * (*fileSize));
    fread(*fileBuffer, sizeof(uint8_t), *fileSize, fp_s);
    fclose(fp_s);

    return 0;
}

uint8_t* einkdmt(uint8_t *srcImg, int width,int height,const char *filename)
{
    printf("Spectra6 rendering library sample\n");

    EinkRenderErrMsg res;
    uint8_t *dstImg;
    uint8_t *lut;
    int lut_size;
    // ============ init ============
    readBinaryFile_JY(filename, &lut, &lut_size);

    res = Eink_Spectra6_render_init(lut, lut_size);

    // ============ read image ============
    dstImg = (uint8_t *)malloc(width * height * 3 * sizeof(uint8_t));

    res = Eink_Spectra6_render(srcImg, EINK_BGR888, dstImg, EINK_BGR888, width, height, DITHER, MODE);

    return dstImg;
}
